<?php

/**
 * This is the model class for table "yii_photo".
 *
 * The followings are the available columns in table 'yii_photo':
 * @property integer $id
 * @property integer $hotel_id
 * @property integer $tour_id
 * @property string $path_to_preview
 * @property string $path_to_original
 * @property string $description
 */
class Photo extends CActiveRecord
{
    public $file;

    const IMAGES_LOCATION = '/photos';
    const TEMP_FILE_NAME = '/temp';

	/**
	 * Returns the static model of the specified AR class.
	 * @return Photo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'yii_photo';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('description', 'required'),
			array('hotel_id, tour_id', 'numerical', 'integerOnly'=>true),
			array('path_to_preview, path_to_original', 'length', 'max'=>255),
			array('path_to_preview, path_to_original, path_to_thumb', 'safe'),
            array('file', 'file', 'types'=>'jpg, gif, png', 'on'=>'insert'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, hotel_id, tour_id, path_to_preview, path_to_original, description', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'hotel_id' => 'Hotel',
			'tour_id' => 'Tour',
			'path_to_preview' => 'Миниатюра',
			'path_to_original' => 'Оригинал',
			'path_to_thumb' => 'Миниатюра',
			'description' => 'Описание',
            'file'=>'Файл',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('hotel_id',$this->hotel_id);
		$criteria->compare('tour_id',$this->tour_id);
		$criteria->compare('path_to_preview',$this->path_to_preview,true);
		$criteria->compare('path_to_original',$this->path_to_original,true);
		$criteria->compare('path_to_thumb',$this->path_to_original,true);
		$criteria->compare('description',$this->description,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    /**
     * Used for searching all photo, related with hotel
     * @param $hotel_id
     * @return CActiveDataProvider
     */
    public function searchForHotel($hotel_id)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('hotel_id',$hotel_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    /**
     * Used for searching all photo, related with tour
     * @param $tour_id
     * @return CActiveDataProvider
     */
    public function searchForTour($tour_id)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('tour_id',$tour_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    /**
     * 
     * @return bool
     */
    public function afterValidate(){
        parent::afterValidate();
        $this->file = CUploadedFile::getInstance($this,'file');

        if($this->file != null){
            $this->appendWithImages();
        }

        return true;
    }

    public function delete(){
        ThumbUtil::deleteThumbByShortPath($this->path_to_preview);
        ThumbUtil::deleteThumbByShortPath($this->path_to_original);
        ThumbUtil::deleteThumbByShortPath($this->path_to_thumb);
        parent::delete();
    }

    private function appendWithImages(){

        $file_name = md5(time().rand());

        $ext = '.'.$this->file->extensionName;
        $file_name = $file_name.$ext;

        $original_path = $this::IMAGES_LOCATION.'/'.$file_name;
        $this->file->saveAs(ThumbUtil::getImagesLocationRoot().$original_path);

        $this->path_to_original = ThumbUtil::IMAGES_LOCATION.$original_path;

        $file_name = md5($file_name).$ext;

        ThumbUtil::createPreview($original_path, $this::IMAGES_LOCATION, $file_name);
        $this->path_to_preview = ThumbUtil::IMAGES_LOCATION.$this::IMAGES_LOCATION.'/'.$file_name;

        $file_name = md5($file_name).$ext;

        ThumbUtil::createSmallThumb($original_path, $this::IMAGES_LOCATION, $file_name);
        $this->path_to_thumb = ThumbUtil::IMAGES_LOCATION.$this::IMAGES_LOCATION.'/'.$file_name;

    }

}